package com.zyb.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyb.commonutils.R;
import com.zyb.eduservice.entity.EduTeacher;
import com.zyb.eduservice.entity.vo.TeacherQuery;
import com.zyb.eduservice.service.EduTeacherService;
import com.zyb.servicebase.exception.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testJava
 * @since 2022-03-27
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/edu-teacher")
//@CrossOrigin
public class EduTeacherController {

    @Resource
    private EduTeacherService eduTeacherService;

    @GetMapping("findAll")
    public R findAll() {
        /*try {
            int num=10/0;
        } catch (Exception e) {
            throw new GuliException(20001,"执行了自定义异常");
        }*/
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items", list);
    }

    //删除讲师
    @DeleteMapping("{id}")
    public R removeTeacher(@PathVariable("id") String id) {
        boolean flag = eduTeacherService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //分页查询讲师的方法
    //current 当前页
    //limit 每页记录数
    @GetMapping("page/{current}/{limit}")
    public R pageListTeacher(@PathVariable("current") Long current, @PathVariable("limit") Long limit) {
        Page<EduTeacher> teacherPage = new Page<>(current, limit);
        eduTeacherService.page(teacherPage, null);
        Long total = teacherPage.getTotal();
        List<EduTeacher> rows = teacherPage.getRecords();
        return R.ok().data("total", total).data("rows", rows);
    }

    //条件查询带分页
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    //使用RequestBody必须使用post提交
    public R pageTeacherCondition(@PathVariable("current") Long current, @PathVariable("limit") Long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        Page<EduTeacher> teacherPage = new Page<>(current, limit);
        //构造条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //多条件进行查询
        // mybatis学过 动态sql
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!(level == null)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_modified", end);
        }
        //进行排序
        wrapper.orderByDesc("gmt_create");
        eduTeacherService.page(teacherPage, wrapper);
        Long total = teacherPage.getTotal();
        List<EduTeacher> rows = teacherPage.getRecords();
        return R.ok().data("total", total).data("rows", rows);
    }

    //添加讲师
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.save(eduTeacher);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //根据id查询
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable("id") String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("teacher", eduTeacher);
    }

    //修改讲师
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }

    }
}
